package org.sucri.floxs.ext.widget;

import org.sucri.floxs.ext.data.DataStore;
import org.sucri.floxs.ext.JsonConfig;
import org.sucri.floxs.ext.Ext_Util;
import org.sucri.floxs.utils.Json_Util;
import net.sf.json.JSONObject;

/**
 *
var grid;
var ds;

Ext.onReady(function(){
	init_grid();
});

function init_grid() {
    ds = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: '/movie/grid_data'}),

        reader: new Ext.data.JsonReader({
            root: 'Movies',
            totalProperty: 'Total',
            id: 'id'
        }, [
            {name: 'title', mapping: 'title'},
            {name: 'plot', mapping: 'plot'},
            {name: 'release_year', mapping: 'date'},
            {name: 'genre', mapping: 'genre'},
            {name: 'mpaa', mapping: 'mpaa'},
            {name: 'directed_by', mapping: 'directed_by'}
        ]),
        // turn on remote sorting
        remoteSort: true
    });

    var cm = new Ext.grid.ColumnModel
    	([{
           id: 'title',
           header: "Title",
           dataIndex: 'title',
           width: 250
        },{
           header: "Release Year",
           dataIndex: 'release_year',
           width: 75
        },{
           header: "MPAA Rating",
           dataIndex: 'mpaa',
           width: 75
        },{
           header: "Genre",
           dataIndex: 'genre',
           width: 100
        },{
           header: "Director",
           dataIndex: 'directed_by',
           width: 150
        }]);

    cm.defaultSortable = true;

    grid = new Ext.grid.Grid('movies_grid', {
        ds: ds,
        cm: cm,
        selModel: new Ext.grid.RowSelectionModel({singleSelect:true}),
		autoExpandColumn: 'title'
    });

    grid.render();
    ds.load({params:{start:0, limit:20}});

 *
 * Created by IntelliJ IDEA.
 * User: Wen Yu
 * Date: Jul 10, 2007
 * Time: 10:05:42 PM
 * To change this template use File | Settings | File Templates.
 */
public class Grid extends ExtWidget
{
  private static final String DATA_STORE      = "ds";
  private static final String COL_MODEL       = "cm";
  private static final String SEL_MODEL       = "selModel";
  private static final String AUTO_EXPAND_COL = "autoExpandColumn";

  private String            mContainer = null;
  private DataStore         mDataStore = null;
  private ColumnModel       mColModel  = null;
  private RowSelectionModel mSelModel  = null;

  public Grid(String c) { super(); setContainer(c); }
  public Grid(String c, DataStore ds, ColumnModel cm)
  {
    super();
    setContainer(c);
    setDataStore(ds);
    setColumnModel(cm);
    setSelModel(new RowSelectionModel());
  }

  public void setContainer(          String s) { mContainer = s; }
  public void setDataStore(       DataStore s) { mDataStore = s; }
  public void setColumnModel(   ColumnModel s) { mColModel  = s; }
  public void setSelModel(RowSelectionModel s) { mSelModel  = s; }
  public void setAutoExpandCol(      String s) { mConfig.put(AUTO_EXPAND_COL, s); }

  public JSONObject toJSONObject()
  {
    JSONObject jo = Ext_Util.putAsString(null, DATA_STORE,        mDataStore);

    jo = Ext_Util.putAsString(jo, COL_MODEL,      mColModel);
    jo = Ext_Util.putAsString(jo, SEL_MODEL,      mSelModel);
    //jo = Ext_Util.putFrom(    jo, AUTO_EXPAND_COL, mConfig);

    return jo;
  }

  @Override
  public String construct()
  {
    return "new Ext.grid.Grid('" + mContainer + "', " + toJSONObject() + ");";
  }
  @Override
  public String render()
  {
    String grid = "var " + getVarName() + " = " + construct();
        grid += getVarName() + ".render();";
        grid += getVarName() + ".getDataSource().load({params:{start:0, limit:20}});";

    return grid;
  }
  public String initFunc(String var)
  {
    String g = null;
    
    mConfig.clear();;

    mConfig.put("ds", mDataStore);
    mConfig.put("cm", mColModel);
    if (mSelModel != null) mConfig.put(SEL_MODEL, mSelModel);

    g  = var + " = new Ext.grid.Grid('" + mContainer + "'," + Json_Util.toJSONString(mConfig) + ");" + br();
    g += var + ".render();" + br();
    g += var + ".getDataSource().load({params:{start:0, limit:20}});" + br();

    g = "function init_grid() {" + br() + g + "};" + br();

    return g;
  }
}
